package com.yunfeisoft.business.controller;

import com.yunfeisoft.base.model.User;
import com.yunfeisoft.business.model.Elder;
import com.yunfeisoft.business.model.OutHospital;
import com.yunfeisoft.business.model.OutHospitalApprovalResult;
import com.yunfeisoft.business.service.inter.OutHospitalApprovalResultService;
import com.yunfeisoft.business.service.inter.OutHospitalService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import com.yunfeisoft.utils.ApiUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: OutHospitalApprovalResultController
 * Description: 老人出院审批记录表Controller
 * Author: Jackie liu
 * Date: 2020-05-13
 */
@RestController
public class OutHospitalApprovalResultController extends BaseController {

    @Autowired
    private OutHospitalApprovalResultService outHospitalApprovalResultService;

    @Autowired
    private OutHospitalService outHospitalService;

    /**
     * 医康部审批
     * @param request
     * @return
     */
    @PostMapping("/web/outHospitalApprovalResult/doctorApproval")
    public Response doctorSave(OutHospitalApprovalResult approvalResult, HttpServletRequest request) {
        // 验证参数
        Validator validator = new Validator();
        validator.required(request, "outHospitalId", "参数错误!");
        validator.required(request, "status", "审批状态为空!");
        validator.required(request, "content", "审批内容为空!");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        // 获取审批关联的图片信息
        String[] attachmentIds = ServletRequestUtils.getStringParameters(request, "attachmentIds");
        // 添加审批记录
        outHospitalApprovalResultService.doctorSave(approvalResult, attachmentIds);
        return ResponseUtils.success("医康部审批成功!");
    }

    /**
     * 护理部审批
     * @param request
     * @return
     */
    @PostMapping("/web/outHospitalApprovalResult/nurseSave")
    public Response nurseSave(HttpServletRequest request) {
        // 验证参数
        Validator validator = new Validator();
        validator.required(request, "outHospitalId", "参数错误!");
        validator.required(request, "status", "审批状态为空!");
        validator.required(request, "content", "审批内容为空!");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        String outHospitalId = ServletRequestUtils.getStringParameter(request, "outHospitalId", null);
        String status = ServletRequestUtils.getStringParameter(request, "status", null);
        String content = ServletRequestUtils.getStringParameter(request, "content", null);
        Map<String, Object> params = new HashMap<>();
        initParams(params, request);
        params.put("outHospitalId", outHospitalId);
        params.put("status", Integer.valueOf(status));
        params.put("content", content);
        // 获取审批关联的图片信息
        String[] attachmentIds = ServletRequestUtils.getStringParameters(request, "attachmentIds");
        // 添加审批记录
        outHospitalApprovalResultService.nurseSave(params, attachmentIds);
        return ResponseUtils.success("护理部审批成功!");
    }

    /**
     * 后勤部审批
     * @param approvalResult
     * @param request
     * @return
     */
    @PostMapping("/web/outHospitalApprovalResult/logisticsSave")
    public Response logisticsSave(OutHospitalApprovalResult approvalResult, HttpServletRequest request) {
        // 验证参数
        Validator validator = new Validator();
        validator.required(request, "outHospitalId", "参数错误!");
        validator.required(request, "status", "审批状态为空!");
        validator.required(request, "content", "审批内容为空!");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        // 获取审批关联的图片信息
        String[] attachmentIds = ServletRequestUtils.getStringParameters(request, "attachmentIds");
        // 添加审批记录
        outHospitalApprovalResultService.logisticsSave(approvalResult, attachmentIds);
        return ResponseUtils.success("后勤部审批成功!");
    }

    /**
     * 院办审批
     * @param approvalResult
     * @param request
     * @return
     */
    @PostMapping("/web/outHospitalApprovalResult/contractSave")
    public Response contractSave(OutHospitalApprovalResult approvalResult, HttpServletRequest request) {
        // 验证参数
        Validator validator = new Validator();
        validator.required(request, "outHospitalId", "参数错误!");
        validator.required(request, "status", "审批状态为空!");
        validator.required(request, "content", "审批内容为空!");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        // 获取审批关联的图片信息
        String[] attachmentIds = ServletRequestUtils.getStringParameters(request, "attachmentIds");
        // 添加审批记录
        outHospitalApprovalResultService.contractSave(approvalResult, attachmentIds);
        return ResponseUtils.success("院办审批成功!");
    }

    /**
     * 添加老人出院审批记录表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/outHospitalApprovalResult/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(OutHospitalApprovalResult record, HttpServletRequest request, HttpServletResponse response) {
        outHospitalApprovalResultService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改老人出院审批记录表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/outHospitalApprovalResult/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(OutHospitalApprovalResult record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        outHospitalApprovalResultService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询老人出院审批记录表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/outHospitalApprovalResult/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        OutHospitalApprovalResult record = outHospitalApprovalResultService.load(id);
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询老人出院审批记录表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/outHospitalApprovalResult/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        initParams(params, request);
        Page<OutHospitalApprovalResult> page = outHospitalApprovalResultService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除老人出院审批记录表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/outHospitalApprovalResult/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        outHospitalApprovalResultService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }

    /**
     * 我的出院审批列表
     * @return
     */
    @RequestMapping(value = "/web/outHospitalApprovalResult/my_list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response myApprovalList(HttpServletRequest request) {
        // 获取参数
        String elderName = ServletRequestUtils.getStringParameter(request, "elderName", null);
        int status = ServletRequestUtils.getIntParameter(request, "status", -1);
        // 获取用户信息
        User user = ApiUtils.getLoginUser();
        Map<String, Object> params = new HashMap<>();
        initParams(params, request);
        // 保存参数
        params.put("elderName", elderName);
        params.put("createId", user.getId());
        if (status > 0) {
            params.put("status", status);
        }
        // 查询 我的审批列表
        Page<OutHospital> page = outHospitalService.myApprovalList(params);
        return ResponseUtils.success(page);
    }
}
